import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { MeetingRoom } from './entities/meeting-room.entity';
import { Like, Repository } from 'typeorm';
import { ListMeetingDto } from './dto/list-meeting.dto';
import { CreateMeetingDto } from './dto/create-meeting.dto';
import { UpdateMeetingDto } from './dto/update-meeting.dto';

@Injectable()
export class MeetingRoomService {
  private logger = new Logger();
  @InjectRepository(MeetingRoom)
  private readonly meetingRoomRepository: Repository<MeetingRoom>;
  async initData() {
    const room1 = new MeetingRoom();
    room1.name = '木星';
    room1.capacity = 10;
    room1.equipment = '白板';
    room1.location = '一层西';

    const room2 = new MeetingRoom();
    room2.name = '火星';
    room2.capacity = 20;
    room2.equipment = '投影仪';
    room2.location = '一层东';

    const room3 = new MeetingRoom();
    room3.name = '水星';
    room3.capacity = 30;
    room3.equipment = '白板+投影仪';
    room3.location = '二层';
    await this.meetingRoomRepository.insert([room1, room2, room3]);
  }
  async list(listMeetDto: ListMeetingDto) {
    const {
      name = '',
      location = '',
      capacity,
      pageSize = 10,
      pageNo = 1,
    } = listMeetDto;
    const skip = pageSize * (pageNo - 1);
    const condition: Record<string, any> = {};
    if (name) {
      condition.name = Like(`%${name}%`);
    }
    if (location) {
      condition.location = location;
    }
    if (capacity) {
      condition.capacity = capacity;
    }
    const [lists, totalCount] = await this.meetingRoomRepository.findAndCount({
      where: condition,
      select: [
        'name',
        'location',
        'capacity',
        'id',
        'createTime',
        'equipment',
        'isBooked',
        'updateTime',
      ],
      skip: skip,
      take: pageSize,
    });
    return { totalCount, lists };
  }
  async create(createMeetingDto: CreateMeetingDto) {
    const { name } = createMeetingDto;
    const exist = await this.meetingRoomRepository.findOne({
      where: { name },
    });
    if (exist) {
      throw new HttpException('会议室已存在', HttpStatus.BAD_REQUEST);
    }
    try {
      await this.meetingRoomRepository.insert(createMeetingDto);
      return { message: '新增成功' };
    } catch (err) {
      this.logger.error(err, MeetingRoomService);
      return { message: '新增失败' };
    }
  }
  async update(updateMeetingDto: UpdateMeetingDto) {
    const { name, id } = updateMeetingDto;
    const exist = await this.meetingRoomRepository.findOne({
      where: { id },
    });
    if (!exist) {
      throw new HttpException('会议室不存在', HttpStatus.BAD_REQUEST);
    }
    const existName = await this.meetingRoomRepository.findOne({
      where: {
        name,
      },
    });
    if (existName) {
      throw new HttpException('该名称已存在', HttpStatus.BAD_REQUEST);
    }
    try {
      await this.meetingRoomRepository.update(id, updateMeetingDto);
      return { message: '修改成功' };
    } catch (err) {
      this.logger.error(err, MeetingRoomService);
      return { message: '修改失败' };
    }
  }
  async deleteById(data: { id: number }) {
    const { id } = data;
    const exist = await this.meetingRoomRepository.findOne({
      where: { id },
    });
    if (!exist) {
      throw new HttpException('会议室不存在', HttpStatus.BAD_REQUEST);
    }
    try {
      await this.meetingRoomRepository.delete(id);
      return { message: '删除成功' };
    } catch (err) {
      this.logger.error(err, MeetingRoomService);
      return { message: '删除失败' };
    }
  }
}
